package com.atguigu.eduSta.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.atguigu.common.Result;
import com.atguigu.eduSta.client.UcenterClient;
import com.atguigu.eduSta.entity.StatisticsDaily;
import com.atguigu.eduSta.mapper.StatisticsDailyMapper;
import com.atguigu.eduSta.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author smday
 * @since 2020-08-03
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    private static final Logger log = LoggerFactory.getLogger(StatisticsDailyServiceImpl.class);


    @Resource
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String date) {

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", date);
        baseMapper.delete(wrapper);

        //得到注册人数
        Result result = ucenterClient.countRegisterNum(date);
        Integer registerNum = (Integer) result.getData().get("registerNum");
        //封装
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(registerNum);
        statisticsDaily.setDateCalculated(date);

        //todo 暂时以随机数模拟人数
        statisticsDaily.setVideoViewNum(RandomUtil.randomInt(100, 200));
        statisticsDaily.setLoginNum(RandomUtil.randomInt(100, 200));
        statisticsDaily.setCourseNum(RandomUtil.randomInt(100, 200));
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);

        List<StatisticsDaily> statisticsDailyList = baseMapper.selectList(wrapper);

        log.info("statisticsDailyList:{}",statisticsDailyList);

        List<String> dateList = statisticsDailyList.stream()
                .map(StatisticsDaily::getDateCalculated)
                .collect(Collectors.toList());

        log.info("dateList:{}",dateList);

        Stream<StatisticsDaily> stream = statisticsDailyList.stream();
        List<Integer> numList = new ArrayList<>();

        switch (type){
            case "login_num":
                numList = stream.map(StatisticsDaily::getLoginNum).collect(Collectors.toList());
                break;
            case "register_num":
                numList = stream.map(StatisticsDaily::getRegisterNum).collect(Collectors.toList());
                break;
            case "video_view_num":
                numList = stream.map(StatisticsDaily::getVideoViewNum).collect(Collectors.toList());
                break;
            case "course_num":
                numList = stream.map(StatisticsDaily::getCourseNum).collect(Collectors.toList());
                break;
            default:
                break;

        }
        log.info("numList:{}",numList);
        Map<String,Object> map = new HashMap<>();
        map.put("dateList",dateList);
        map.put("numList",numList);
        return map;
    }
}
